package com.yjm.test.Spring5.ch6;

import java.sql.Connection;
import java.sql.Driver;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Lazy;
import org.springframework.context.annotation.PropertySource;
import org.springframework.context.support.GenericApplicationContext;
import org.springframework.context.support.GenericXmlApplicationContext;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.jdbc.datasource.SimpleDriverDataSource;

@Configuration
@PropertySource("classpath:db/jdbc.properties")
public class DbConfig {
	@Value("${jdbc.driverClassName}")
	private String driverClassName;
	@Value("${jdbc.url}")
	private String url;
	@Value("${jdbc.username}")
	private String username;
	@Value("${jdbc.password}")
	private String password;
	
	//@Bean
	public static PropertySourcesPlaceholderConfigurer
	propertySourcesPlaceholderConfigurer() {
		return new PropertySourcesPlaceholderConfigurer();
	}
	
	@Lazy
	@Bean
	public DataSource dataSource() {
		SimpleDriverDataSource dataSource = new SimpleDriverDataSource();
		try {
			System.out.println("driverClassName:" + driverClassName + " Url:" + url + " username:" + username + " password:" + password);
			Class<? extends Driver> driver = (Class<? extends Driver>) Class.forName(driverClassName);
			dataSource.setDriverClass(driver);
			dataSource.setUrl(url);
			dataSource.setUsername(username);
			dataSource.setPassword(password);
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
			return null;
		}
		return dataSource;
	}
	
	public static void testXmlPlaceholder() {
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:spring/ch6/spring-jdbc.xml");
		ctx.refresh();
		
		DataSource dataSource = ctx.getBean("dataSource", DataSource.class);
		testDataSource(dataSource);
		ctx.close();
	}
	public static void testXmlUtil() {
		GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
		ctx.load("classpath:spring/ch6/spring-jdbc-util.xml");
		ctx.refresh();
		
		DataSource dataSource = ctx.getBean("dataSource", DataSource.class);
		testDataSource(dataSource);
		ctx.close();
	}
	public static void testAnnotation() {
		GenericApplicationContext ctx = new AnnotationConfigApplicationContext(DbConfig.class);

		DataSource dataSource = ctx.getBean("dataSource", DataSource.class);
		testDataSource(dataSource);
		ctx.close();
	}
	public static void testDataSource(DataSource dataSource) {
		Connection connection = null;
		try {
			connection = dataSource.getConnection();
			PreparedStatement statement = connection.prepareStatement("select 1");
			ResultSet resultSet = statement.executeQuery();
			while (resultSet.next()) {
				int mockVal = resultSet.getInt("1");
				System.out.println("testDataSource got " + mockVal);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		System.out.println("xmlPlaceholder--------------");
		testXmlPlaceholder();
		System.out.println("xmlUtil       --------------");
		testXmlUtil();
		System.out.println("Annotation    --------------");
		testAnnotation();
	}
}
